import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import { getAllExamineContent } from '@/api/tag.js'
import { useClassStore } from '@/stores/index.js'
import { ref, onMounted, nextTick, reactive,computed } from 'vue'; 

const subInfo = reactive({
  syllabusId:0,
  subId:0,//可以根据路由拿到
  class_name:'',
  grade_name:'',
  subject:'',
})
// //一进入页面，则把该班级的大纲id给页面
// const classMessage = useClassStore()
// // syllabusId.value = classMessage.classInfo.id
// subInfo.syllabusId = classMessage.classInfo.user_and_acs_id//大纲id
// subInfo.class_name = classMessage.classInfo.class_name
// subInfo.grade_name = classMessage.classInfo.grade_name
// subInfo.subject = classMessage.classInfo.subject
// subInfo.subId = classMessage.classInfo.subId

const routes = [
  {
    path:'/',
    name:'Home',
    component:() => import('@/views/HomePage/index.vue')
  },
  {
    path: '/login',
    name:'Login',
    meta: { title: '登录' },
    component: () => import('@/views/Login/index.vue')
  },
  {
    path: '/layout',
    name: 'Layout',
    component: () => import('@/views/Layout/LayoutTest.vue'),
    children: [
      {
        path: '/layout',
        name:'Subject',
        meta: { title: '科目管理' },
        component: () => import('@/views/subjectManagement/index.vue'),
      },
      {
        path: '/manage',
        component: () => import('@/views/childrenApp/index.vue'),
        redirect: 'Syllabus',
        name: 'HomeSubject',
        meta: { title: '科目管理' },
        children: [
          {
            path: 'syllabus/:subId',
            name: 'Syllabus',
            meta: { title: '教学大纲' },
            component: () => import('@/views/CourseManagement/syllabus/index.vue'),
          },
          {
            path: 'questionTypes/:subId',
            name: 'QuestionTypes',
            meta: { title: '试卷依赖' },
            component: () => import('@/views/paperItemInfoInput/index.vue')
          },
          {
            path: 'talent/:subId',
            name: 'Talent',
            meta: { title: '人才培养计划' },
            component: () => import('@/views/CourseManagement/talent/index.vue')
          },
          {
            name: "TargetManage",
            path: '/manage/:subId',
            meta: { title: '课程考核' },
            component: () => import('@/views/examineTarget/index.vue')
          },
          {
            name: "TagManage",
            path: 'tag/:subId',
            meta: { title: '标签管理' },
            component: () => import('@/views/tagManagement/index.vue')
          },
          {
            path: 'title/analysis/:subId',
            name: 'Analysis',
            meta: { title: '题库分析' },
            component: () => import('@/views/itemBank/analysis.vue')
          },
          {
            path: 'title/:subId',
            name: 'Title',
            meta: { title: '题目汇总' },
            // beforeEnter: (to, from, next) => {
            //   checkTagData(next);
            // },
            component: () => import('@/views/itemBank/index.vue')
          },
          {
            name: "paperList",
            path: 'paper/list/:subId',
            meta: { title: '试卷列表' },
            component: () => import('@/views/paperList/index.vue'),
          },
          {
            name: "paperManage",
            path: 'paperManage/:subId/:paperId',
            meta: { title: '试卷列表' },
            component: () => import('@/views/paperManage/index.vue')
          },
          // {
          //   name: "Syllabus",
          //   path: 'syllabus/:subId',
          //   meta: { title: '教学大纲' },
          //   component: () => import('@/views/CourseManagement/instructionalProgramme/index.vue')
          // },
          {
            name: "paperAnalysis",
            path: 'paperAnalysis/:subId',
            meta: { title: '分数总览' },
            component: () => import('@/views/paperAnalysis/index.vue')
          },
          {
            name: "objectAnalysis",
            path: 'objectAnalysis/:subId',
            meta: { title: '达成度分析' },
            component: () => import('@/views/objectAnalysis/index.vue'),
            beforeEnter: (to, from, next) => {
              checkUploadStatus(next);
            }
          },
          {
            name: "approvalForm",
            path: 'approvalForm/:subId',
            meta: { title: '命题审批表' },
            component: () => import('@/views/approvalForm/index.vue'),
            //  beforeEnter: (to, from, next) => {
            //   checkUploadStatus(next);
            // }
          },
          {
            name: "Training",
            path: 'training/:subId',
            meta: { title: '培养目标' },
            component: () => import('@/views/training/index.vue')
          },
          {
            name: "Certification",
            path: 'certification/:subId',
            meta: { title: '认证体系' },
            component: () => import('@/views/certification/index.vue')
          },
        ]
      },
      {
        path: 'personal',
        name:'Personal',
        meta: { title: '个人中心' },
        component: () => import('@/views/personalCenter/index.vue'),
      }
    ]
  },
]
// 创建 router 实例
const router = createRouter({
  // history: createWebHistory('/h5'),
  // history: createWebHistory(import.meta.env.BASE_URL),
  history: createWebHashHistory(),
  routes
})

router.beforeEach(async (to, from, next) => {
  try {
    // 异步获取 Pinia store 数据
    const classMessage = useClassStore();
    // await classMessage.$fetch(); // 假设使用了 fetch 方法获取数据
    subInfo.syllabusId = classMessage.classInfo.user_and_acs_id//大纲id
    subInfo.class_name = classMessage.classInfo.class_name
    subInfo.grade_name = classMessage.classInfo.grade_name
    subInfo.subject = classMessage.classInfo.subject
    subInfo.subId = classMessage.classInfo.subId
    // 获取到数据后继续导航
    next();
  } catch (error) {
    console.error('Error fetching Pinia store data:', error);
    // 处理错误情况
    next(error); // 可以传递错误对象给路由导航
  }
});

const checkUploadStatus=(next)=> {
  const current_ACSID = localStorage.getItem('current_ACSID');
  const hasUploadedData = localStorage.getItem(`uploadedData${current_ACSID}`);
  if (!hasUploadedData) {
    ElMessageBox.alert('请先在分数总览页按照模板上传期末成绩后再访问该页面', '提示', {
      confirmButtonText: '确定',
      callback: action => {
        next({ name: 'paperAnalysis' }); 
      }
    });
    next({ name: 'paperAnalysis' }); // 如果没有上传数据，则重定向到分数总览
  } else {
    next(); // 如果有上传数据，则允许访问子页面
  }
}

//检查标签有没有数据
const checkTagData = async (next) =>{
  const res = await getAllExamineContent(subInfo.subId)
  console.log(res.data.data,'dadadadares');
  if(res.data.data.length === 0){
    ElMessageBox.alert('请先在标签管理页面上添加标签后再访问该页面', '提示', {
      confirmButtonText: '确定',
      callback: action => {
        next({ name: 'paperAnalysis' }); 
      }
    });
    next({ name: 'TagManage' }); // 如果没有上传数据，则重定向到分数总览
  }else{
    next(); // 如果有上传数据，则允许访问子页面

  }
}
export default router


